/* $Id: TextDetail.java,v 1.2 2003/08/02 19:20:02 survey Exp $
 * 
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */


package org.medbeans.modules.search.types;


import javax.swing.JEditorPane;
import javax.swing.SwingUtilities;
import javax.swing.text.Caret;

import org.openide.cookies.EditorCookie;
import org.openide.cookies.LineCookie;
import org.openide.loaders.DataObject;
//import org.openide.nodes.Node;
import org.openide.text.Annotation;
import org.openide.text.Line;
import org.openide.util.NbBundle;


/** 
 * Holds details about one search hit in the text document.
 *
 * @author Tomas Pavek
 */
public class TextDetail extends Object {

    /** Property name which indicates this detail to show. */
    public static final int DH_SHOW = 1;
    /** Property name which indicates this detail to go to. */
    public static final int DH_GOTO = 2;
    /** Property name which indicates this detail to hide. */
    public static final int DH_HIDE = 3;
    
    /** Data object. */
    private DataObject dobj;
    /** Line number where search result occures.*/
    private int line;
    /** Text of the line. */ 
    private String lineText;
    /** Column where search result starts. */
    private int column;
    /** Length of search result which to mark. */
    private int markLength;
    /** Line. */
    private Line lineObj;
    /** Annotation used for this detail. */
    private Annotation annotation;
    
    
    /** Constructor using data object. */
    public TextDetail(DataObject dobj) {
        this.dobj = dobj;
    }

    /** Shows the search detail on the DataObject.
     * The document is opened in the editor, the caret is positioned on the right line and column 
     * and searched string is marked.
     * @param how indicates how to show detail. 
     * @see #DH_GOTO 
     * @see #DH_SHOW 
     * @see #DH_HIDE */
    public void showDetail(int how) {
        if(dobj == null) return;
        
        if(lineObj == null) { // try to get Line from DataObject
            LineCookie lineCookie = (LineCookie)dobj.getCookie(LineCookie.class);
            if(lineCookie != null)
                lineObj = lineCookie.getLineSet().getOriginal(line-1);

            if(lineObj == null) return;
        }

        if (column > 0) { // exact position on the line is known
            if (how == DH_HIDE) return;

            if (how == DH_SHOW)
                lineObj.show(Line.SHOW_TRY_SHOW, column-1);
            else if (how == DH_GOTO)
                lineObj.show(Line.SHOW_GOTO, column-1);

            if (markLength > 0) {
                EditorCookie edCookie = (EditorCookie)dobj.getCookie(EditorCookie.class);
                if (edCookie != null) {
                    JEditorPane[] panes = edCookie.getOpenedPanes();
                    if (panes != null && panes.length > 0) {
                        final Caret caret = panes[0].getCaret();

                        // Necessary since above lineObj.show leads to invoke later as well.
                        SwingUtilities.invokeLater(new Runnable() {
                            public void run() {
                                caret.moveDot(caret.getDot() + markLength);
                            }
                        });
                    }
                }
            }
        } else { // go to the line without column specified
            if (how == DH_SHOW) {
                getAnnotation().attach(lineObj);
            } else if (how == DH_GOTO) {
                getAnnotation().detach();
            } else if (how == DH_HIDE) {
                getAnnotation().detach();
            }
        }
    }

    /** Gets annotation used for this detail. */
    private synchronized Annotation getAnnotation() {
        if(annotation == null) {
            annotation = new Annotation() {
                public String getAnnotationType() {
                    return "org-netbeans-modules-search-RegExpAnnotation"; // NOI18N
                }
                
                public String getShortDescription() {
                    return NbBundle.getBundle(TextDetail.class).getString("LBL_AnnotationShortDesc");
                }
            };
        }
        
        return annotation;
    }
    
    /** Getter for <code>lineText</code> property. */
    public String getLineText() {
        return lineText;
    }
    
    /** Setter for <code>lineText</code> property. */
    public void setLineText(String text) {
        lineText = text;
    }
    

    /** Gets the <code>DataObject</code> where the searched text was found. 
     * @return data object or <code>null</code> if no data object is available */
    public DataObject getDataObject() {
        return dobj;
    }

    /** Gets the line position of the text. */
    public int getLine() {
        return line;
    }

    /** Sets the line position of the text. */
    public void setLine(int line) {
        this.line = line;
    }

    /** Gets the column position of the text. */
    public int getColumn() {
        return column;
    }

    /** Sets the column position of the text. */
    public void setColumn(int col) {
        column = col;
    }

    /** Gets the length of the text that should be marked when the detail is shown. */
    public void setMarkLength(int len) {
        markLength = len;
    }

}
